package Final;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Panel extends JPanel{
  
	
	private Matriz m;
	private int x, y;
	private final int Height = 30;
	private final int Width = 30;
	private BufferedImage image;
	private static Vector<Object> objetos;

	public Panel(){
		super();
		addMouseListener(new MouseAdapter() { public void mousePressed (MouseEvent e) { raton(e); } } );
		m = new Matriz();
		x = m.getCol();
		y = m.getFil();
		setBackground(Color.GRAY);
		objetos = new Vector<Object>();
		objetos.add("Robot");
		objetos.add("Obstaculo");
		objetos.add("fondo");
		repaint();
	}
	
	public void paintComponent(Graphics g) {
	    super.paintComponent(g);
	    Graphics2D g2 = (Graphics2D)g;
	    for (int i = 0; i < x; i++) {
	      for (int j = 0; j < y; j++) {
	    	image = m.matriz[j][i].getImage();
	        g2.drawImage(image, i*this.Height, j*this.Width, this.Width-1, this.Height-1, this);
	        
	      }
	    }
	}
	
	private void raton(MouseEvent e) {
		Point point = e.getPoint();
		int xpos = (int)(point.getX()/ this.Height);
		int ypos = (int)(point.getY()/ this.Width);
		
		if((xpos < x) && (ypos < y))
			if(e.isMetaDown()){
				int iaux = m.matriz[ypos][xpos].getNumImage();
				m.matriz[ypos][xpos].setDefault();
				switch(iaux){
				case Elemento.ROBOT: objetos.add("Robot");
					break;
				case Elemento.OBSTACULO: objetos.add("Obstaculo");
					break;
				}			
			}else{
				String Saux;
				Object[] auxObjetos = objetos.toArray();
				Object Oaux = JOptionPane.showInputDialog(Panel.this, "Seleccione opcion","Selector de objetos",JOptionPane.QUESTION_MESSAGE, null, auxObjetos , "fondo");
				if(Oaux != null){
					Saux = (String)Oaux + ".jpg";
					try {   
						if(!Saux.equals("fondo.jpg")){
							image = ImageIO.read(new File(Saux));
							m.matriz[ypos][xpos].setImage(image);
							m.matriz[ypos][xpos].setEstado(false);
							if (Saux.equals("Robot.jpg")){
								m.matriz[ypos][xpos].setNumImage(Elemento.ROBOT);
								removeVectorElement("Robot");
							}
							if (Saux.equals("Obstaculo.jpg")){
								m.matriz[ypos][xpos].setNumImage(Elemento.OBSTACULO);
								removeVectorElement("Obstaculo");
							}
						}
					} catch (IOException ex) {
						// handle exception...
					}
				}
			}
		repaint();
	}
	
	public void reset(){
		for (int i = 0; i < y; i++)
			for (int j = 0; j < x; j++){
				if(!m.matriz[i][j].getEstado()){
					int iaux = m.matriz[i][j].getNumImage();
					m.matriz[i][j].setDefault();
					switch(iaux){
					case Elemento.ROBOT: objetos.add("Robot");
						break;
					case Elemento.OBSTACULO: objetos.add("Obstaculo");
						break;
					}
				}

			}
		repaint();
	}
	
//	public boolean cargarMatriz(String namefile){
//		boolean result = m.cargarMatriz(namefile);	
//		y = m.getFil();
//		x = m.getCol();
//
//		return result;
//	}
	public boolean cargarMatriz(String namefile){
		boolean result = m.cargarMatriz(namefile);	
		
		y = m.getFil();
		x = m.getCol();

		return result;
	}
	
	public boolean guardarMatriz(String namefile){ 
		return m.guardarMatriz(namefile); 
	}
	
	public void setFil(int _fil){
		y = _fil;
		m.setFil(_fil);
	}
	public void setCol(int _col){
		x = _col;
		m.setCol(_col); 
	}
	public int getFil(){ 
		return y; 
	}
	public int getCol(){ 
		return x; 
	}
	
	public static void removeVectorElement(String str){
		objetos.remove(str);
	}
	
}
